package com.frank.loadinglibrary.special.cool.letter;


import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;

/**
 * Created by Weiwu on 16/2/19.
 */
public class OLetter extends Letter {

    private Paint mPaint;
    private RectFloat mRectF;
    //启动角度
    private int mStartAngle;
    //偏移角度
    private int mSweepAngle;

    private AnimatorValue mCirAnimator;

    public OLetter(float x, float y) {
        super(x, y);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mRectF = new RectFloat(mCurX - 60, mCurY - 60, mCurX + 60, mCurY + 60);
    }

    @Override
    public void reset() {
        super.reset();
        if (mPaint != null) {
            mPaint.reset();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL_STYLE);
        }
        mStartAngle = 0;
        mSweepAngle = 0;
        if (mRectF != null) {
            mRectF.modify(mCurX - 60, mCurY - 60, mCurX + 60, mCurY + 60);
        }
    }

    @Override
    public void startAnim() {
        mCirAnimator = new AnimatorValue();
        mCirAnimator.setDuration(mDuration);
        mCirAnimator.setValueUpdateListener((ani, v) -> {
            float factor = (float) v;
            if (factor < 0.5f) {
                mStartAngle = (int) (90 * factor);
                mSweepAngle = (int) (180 * factor);
            } else {
                float zoroToOne = (float) ((factor - 0.5) * 2);
                mStartAngle = (int) (45 + 135 * zoroToOne);
                mSweepAngle = (int) (90 + 270 * zoroToOne);
            }
        });
        mCirAnimator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (animationCallBack != null) {
                    animationCallBack.onLetterAnimatorEnd();
                }
            }
        });
        mCirAnimator.start();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        canvas.drawArc(mRectF, new Arc(mStartAngle, mSweepAngle, false), mPaint);
    }
}
